<?
class Localization {

  var $local;
  var $words;

  function Localization($local = "en") {
    $this->local = $local;
    $this->load();
  }

  function load(){
    $fp = fopen( ADMIN.'/localization/local.'.$this->local, 'r');
    $all = "";

    while ($line = fgets ($fp)) {
      $all.=$line;
    }
    fclose($fp);

    $word = explode("#",$all);
    for($i = 1; $i < count($word); $i++){  // for($i = 0; $i < count($word); $i++) ==> error : Notice: Undefined offset: 1
      $a = explode("===",$word[$i]);
      $this->words[$a[0]] = stripcslashes($a[1]);
    }
  }

  function sortword(){
    
  }

  function save($txt){

    $fp = fopen( 'local.'.$this->local, 'w+');

    fwrite($fp,$txt);
    fclose($fp);

  }

  function del($del){

    $fp = fopen( 'local.'.$this->local, 'r');

    $txt = "";
    $all = "";
    while ($line = fgets ($fp)) {
      $all.=$line;
    }

    $word = explode("#",$all);
    for($i = 0; $i < count($word); $i++){
      $a = explode("===",$word[$i]);
      if($a[0] != $del){
        $txt .= "#".$word[$i];
      }
    }

    fclose($fp);

    $fp = fopen( 'local.'.$this->local, 'w');
    fwrite($fp,$txt);
    fclose($fp);
  }

  function add($word, $display){

    $fp = fopen( 'local.'.$this->local, 'a+');

    $txt .= "#".$word."===".$display."\n";

    fwrite($fp,$txt);
    fclose($fp);
  }

  function get($w){
    //if($this->words[strtolower($w)] == null){ // XAMMP HAVE ERROR
    if(!array_key_exists($w,$this->words)){ // array_key_exists (PHP 4 >= 4.0.7, PHP 5)
      return $w;
    }
    return $this->words[strtolower($w)];
  }

  function reverse($w){

    foreach($this->words as $key => $value){
      if(trim($value) == $w){
        return $key;
      }
    }
    return "";
  }
}

function _l($w){
  global $local;

  echo str_replace("\n","",$local->get($w));
}

function __l($w){
  global $local;

  return str_replace("\n","",$local->get($w));
}

function _g($w){
  echo __g($w);
}

function __g($w){
  $words = explode(",",$w);
   $t = "";

  for($k=0; $k<count($words); $k++){
    $words[$k] = trim($words[$k]);
    if($k == count($words)-1){
      $t .= __l($words[$k]);
    }else{
      $t .= __l($words[$k]);
      $t .= ", ";
    }
  }
  return $t;
}

function __r($w){
  global $local;

  return str_replace("\n","",$local->reverse($w));
}

function _r($w){
  global $local;

  echo str_replace("\n","",$local->reverse($w));
}

?>